/*
Write a program to verify that duplicated file descriptors share a file offset value
and open file status flags.
*/

#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>

int
main (int argc, char *argv[])
{
  if (argc != 2) return EXIT_FAILURE;

  int fd;
  if ((fd = open (argv[1],
                  O_CREAT|O_WRONLY|O_TRUNC, S_IRWXU|S_IRWXG|S_IRWXO)) == -1)
    return EXIT_FAILURE;

  int dup_fd;
  if ((dup_fd = dup (fd)) == -1) return EXIT_FAILURE;

  assert (lseek (fd, 0, SEEK_CUR) == lseek (dup_fd, 0, SEEK_CUR));
  write (fd, "from fd\n", 8);
  assert (lseek (fd, 0, SEEK_CUR) == lseek (dup_fd, 0, SEEK_CUR));
  write (dup_fd, "from dup_fd\n", 12);
  assert (lseek (fd, 0, SEEK_CUR) == lseek (dup_fd, 0, SEEK_CUR));

  return EXIT_SUCCESS;
}
